<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\model;

use think\model\concern\SoftDelete;

/**
 * 应用程序模型文件
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class CloudMarketProgram extends CloudMarketBaseModel
{
    // 配置软删除、启用回收站
    use SoftDelete;
    protected $deleteTime = "delete_time";

    // 字段自动类型转换
    protected $type = array(

    );
    // 数据输出需要追加的属性
    protected $append = array(

    );
    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }

    /**
     * 当前版本
     * @param string $version
     */
    public function getUpdateProgram($version = '')
    {
        return $this->getProgram(1, $version);
    }

    /**
     * 获取程序
     * @param $type
     * @param string $version
     * @return array|false|\think\Collection|\think\model\Collection
     */
    public function getProgram($type, $old_version = '', $version = '', $is_all = true)
    {
        if (!$version) {
            //不存在版本号看用户对应的版本
            if ($old_version) {
                //不存在对应的版本
                $program = $this->where(['type' => $type, 'open_switch' => 1])->where('old_version', $old_version)->find();
            } else {
                //直接使用最新的版本
                $program = $this->where(['type' => $type, 'open_switch' => 1])->order('id','desc')->find();
            }
        } else {
            //指定了版本号就直接用
            $program = $this->where(['type' => $type, 'open_switch' => 1, 'version' => $version])->find();
        }
        if (!$program) {
            return false;
        } else {
            if ($is_all) {
                $programs = $this->where('id', '>=', $program['id'])->where(['type'=>$type])->order('id','desc')->select();
                foreach ($programs as $key => $value) {
                    $programs[$key] = $this->formatProgram($value);
                }
                return $programs;
            } else {
                return $this->formatProgram($program);
            }
        }
    }

    public function formatProgram($program)
    {
        $program = $program->toArray();
        // TODO 图片地址
//        $program['file_size'] = (new Attachment())->where(['url'=>$program['down_file']])->value('filesize');
        $program['down_file'] = cdnurl($program['down_file']);
        unset($program['update_time']);
        $program['timestamp'] = date("Y-m-d", strtotime($program['create_time']));
        $program['version_detailed'] = "v" . $program['version'] . '(' . date("Ymd", strtotime($program['create_time'])) . sprintf("%04d", $program['id']) . ')';
        return $program;
    }


    public function getDownFileAttr($val)
    {
        // 如果值为空，直接返回
        if (empty($val)) {
            return $val;
        }

        // 检查是否已经包含 http 或 https 前缀
        if (!preg_match('/^http(s)?:\/\//i', $val)) {
            // 添加前缀
            $val = 'https://dkyx-1251560393.cos.ap-chengdu.myqcloud.com' . $val;
            $this->down_file = $val;
            $this->save();
        }

        return $val;
    }
}
